<?php
/**
 * 后台 企业家访谈 控制器
 *
 */
class InterviewAction extends Action
{
    function _initialize()
    {
        
    }
	/**
	 * 管理所有的企业家访谈
	 */
    public function AdminIndex()
    {
    	EasyACL('Interview','AdminIndex',$_SESSION);
    	$Interview = D('Interview');
    	$count = $Interview->count(NULL,'iid');
    	$listRows = 15;
    	$fields = 'iid,title,name,interview_sort';
    	import('ORG.Util.Page');
    	$p = new Page($count,$listRows);
    	$list = $Interview->findall(NULL,$fields,'iid desc',$p->firstRow .','. $p->listRows);
    	$page = $p->show();
    	
    	//To templates
    	$this->assign('Inters',$list);
    	$this->assign('page',$page);
    	$this->display();
    }
    
    /**
     * 创建新的企业家访谈
     */
    public function AdminCreate()
    {
    	EasyACL('Interview','AdminCreate',$_SESSION);
    	//POST
    	if ($this->isPost()) {
    		$Interview = D('Interview');
    		if ($Interview->create()) {
    			if ($Interview->add()) {
    				if ($_FILES['file']['name'] != '') {
    					$Iid = $Interview->getLastInsID();
    					$this->U($Iid);
    				} else {
    					return $this->success('添加成功!');
    				}
    			} else {
    				return $this->error($Interview->getError());
    			}
    		} else {
    			return $this->error($Interview->getError());
    		}
    	}
    	//To tpl
    	$this->display();
    }
    
    /**
     * 修改企业家访谈
     */
    public function AdminUpdate()
    {
    	EasyACL('Interview','AdminUpdate',$_SESSION);
    	$Interview = D('Interview');
    	import('ORG.Util.HashMap');
    	$conditions = new HashMap();
    	
    	$Input = Input::getInstance();
    	$Input->filter('addslashes');
    	
    	if ($this->isPost()) {
    		$conditions->put('iid',array('EQ',$Input->post('iid')));
    		$Interview->where($conditions)->find();
    		if ($Interview->create()) {
    			if ($Interview->save()) {
    				if ($_FILES['file']['name'] == '') {
    					return $this->success('修改成功');
    				} else {
    					unlink($Input->post('img'));
    					$this->U($Input->post('iid'));
    				}
    			} else {
    				return $this->error($Interview->getError());
    			}
    		} else {
    			return $this->error($Interview->getError());
    		}
    	}
    	
    	//获取要修改的数据
    	$conditions->put('iid',array('EQ',$Input->get('iid')));
    	$Inter = $Interview->where($conditions)->find();
    	
    	//到模板
    	$this->assign('Interview',$Inter);
    	$this->display();
    }
    
    /**
     * 删除企业家访谈
     */
    public function AdminDel()
    {
    	EasyACL('Interview','AdminDel',$_SESSION);
    	$Input = Input::getInstance();
    	$Input->filter('addslashes');
    	
    	$conditions['iid'] = $Input->get('iid');
    	$Interview = D('Interview');
    	$I = $Interview->where($conditions)->find();
    	unlink($I['img']);
    	
    	if ($Interview->deleteById($Input->get('iid'))) {
    		return $this->success('删除成功');
    	} else {
    		return $this->error('尝试删除失败');
    	}
    }
    
    /**
     * 批量删除
     */
    public function AdminDels()
    {
    	EasyACL('Interview','AdminDels',$_SESSION);
    }
    
    /**
     * 上传图片
     */
    public function U($id)
    {
    	EasyACL('Interview','U',$_SESSION);
		//上传文件
		import("ORG.Net.UploadFile");
		$upload = new UploadFile();
		//设置上传文件大小
		$upload->maxSize  = 266240;
		//设置上传文件类型
		$upload->allowExts  = explode(',','jpg,gif,png');
		//设置附件上传目录
		$upload->savePath =  './upload/Interview/'; 
		
		//执行上传操作
		if(!$upload->upload()) {
			$this->error($upload->getError());
		}else{ 
			$AllFiles = $upload->getSuccessFiles();
			$conditions['iid'] = $id;
			$Interview = D('Interview');
			$Interview->img = './upload/Interview/'.$AllFiles[0]['savename'];
			if ($Interview->save()) {
				return $this->success('操作成功!');
			} else {
				return $this->error($Interview->getError());
			}
		}
    }
}
?>





